package com.test.nnooolwdeaer.view_wooden.module_wooden;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.test.nnooolwdeaer.R;
import com.test.nnooolwdeaer.bean_wooden.user_info_wooden.CoteBankAccountList_wooden;
import com.test.nnooolwdeaer.constant_wooden.Constant_wooden;
import com.test.nnooolwdeaer.dialog_wooden.AddAccountDialog2_Cote_wooden;
import com.test.nnooolwdeaer.dialog_wooden.AddBankAccountDialog_Cote_wooden;
import com.test.nnooolwdeaer.dialog_wooden.DeleteBankAccountDialog_Cote_wooden;
import com.test.nnooolwdeaer.util_wooden.ThreadUtil_wooden;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class AddBankAccountView_Cote_wooden extends LinearLayout {

    private LinearLayout groupLayout_wooden;
    private View addAccountBtnView_wooden;

    private final ArrayList<View> accountArrViews_wooden = new ArrayList<>();

    public static boolean addAccountFlag_wooden =false;

    public AddBankAccountView_Cote_wooden(Context context) {
        super(context);
        init_wooden(context);
    }

    public AddBankAccountView_Cote_wooden(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init_wooden(context);
    }

    public AddBankAccountView_Cote_wooden(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init_wooden(context);
    }

    private void init_wooden(Context context){

        View view = View.inflate(context, R.layout.view_add_bank_account_cote_wooden,this);
        groupLayout_wooden = view.findViewById(R.id.view_add_bank_account_cote_layout_group);
        addAccountBtnView_wooden = view.findViewById(R.id.view_add_bank_account_cote_layout_add_account_btn);

        addAccountBtnView_wooden.setOnClickListener(v -> {
            if (CoteBankAccountList_wooden.getInstance_wooden().getBankAccountList_wooden().size()>0){
                new AddAccountDialog2_Cote_wooden(context, CoteBankAccountList_wooden.getInstance_wooden().getBankAccountList_wooden().get(0)).show();
            }else {
                new AddBankAccountDialog_Cote_wooden(context).show();
            }
        });

    }

    //更新账户列表
    public void updateAccountList_wooden(){
        accountArrViews_wooden.clear();
        groupLayout_wooden.removeAllViews();
        ArrayList<CoteBankAccountList_wooden.CoteBankAccount> accounts = CoteBankAccountList_wooden.getInstance_wooden().getBankAccountList_wooden();
        for (CoteBankAccountList_wooden.CoteBankAccount account:accounts){
            addAccount_wooden(account);
        }
        if (accountArrViews_wooden.size()==1) accountArrViews_wooden.get(0).findViewById(R.id.item_wallet_account_ib_delete).setVisibility(GONE);

        if (addAccountFlag_wooden){
            addAccountFlag_wooden = false;
            if (CoteBankAccountList_wooden.getInstance_wooden().getBankAccountList_wooden().size()==1){
                new AddAccountDialog2_Cote_wooden(getContext(), CoteBankAccountList_wooden.getInstance_wooden().getBankAccountList_wooden().get(0)).show();
            }
        }
    }

    private void addAccount_wooden(CoteBankAccountList_wooden.CoteBankAccount bankAccount){

        View view = View.inflate(getContext(), R.layout.item_wallet_account_cote_wooden, null);
        TextView accountTv = view.findViewById(R.id.item_wallet_account_tv_account);
        ImageView accountTypeIv = view.findViewById(R.id.item_wallet_account_iv_wallet_type);
        TextView accountTypeTv = view.findViewById(R.id.item_wallet_account_tv_wallet_type);
        ImageButton deleteIb = view.findViewById(R.id.item_wallet_account_ib_delete);

        accountArrViews_wooden.add(view);

        accountTv.setText(bankAccount.getWalletMobile());
        accountTypeTv.setText(bankAccount.getWalletDesc());
        deleteIb.setOnClickListener(v -> new DeleteBankAccountDialog_Cote_wooden(getContext(),bankAccount).show());

        groupLayout_wooden.addView(view);

        //获取图标
        if (bankAccount.getBitmap()!=null){
            accountTypeIv.setImageBitmap(bankAccount.getBitmap());
        }else {
            ThreadUtil_wooden.getInstance_wooden().runOnChildThread_wooden(() -> {
                try {
                    if (TextUtils.isEmpty(bankAccount.getWalletLogo())) return;
                    URL url = new URL(bankAccount.getWalletLogo());
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(5000);
                    connection.setRequestMethod("GET");
                    if (connection.getResponseCode()==200){
                        InputStream inputStream = connection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        bankAccount.setBitmap(bitmap);
                        ThreadUtil_wooden.getInstance_wooden().runOnUiThread_wooden(() -> {
                            if (accountTypeIv!=null) accountTypeIv.setImageBitmap(bitmap);
                        });
                    }
                } catch (IOException e) {
                    if (Constant_wooden.Debug) e.printStackTrace();
                }
            });
        }

    }

}
